﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using KPIS.Utils;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
	public partial class PS_HisHealthIssue : System.Web.UI.UserControl
	{
		public object DataItem { get; set; }
		private readonly IHealthBLL hhcBll = new HealthBLL();
		private readonly Main main = new Main();
		private readonly SsMng ssMng = new SsMng(); 
		private HealthInfo healthInfo = new HealthInfo();

		protected void Page_Load(object sender, EventArgs e)
		{
			if (DataItem == null) return;

			ButtonUpdate_.Visible = !(DataItem is GridInsertionObject);
			submitInsert_.Visible = (DataItem is GridInsertionObject);

			if(DataItem is GridInsertionObject)
			{
				healthInfo.Sequence = Convert.ToInt32(DataBinder.Eval(DataItem, "Sequence"));
				object dbEs =  DataBinder.Eval(DataItem, "EmployeeSequence");
				int intEs = dbEs is System.DBNull ? -1 : Convert.ToInt32(dbEs);
				healthInfo.Employee = new EmployeeInfo(intEs);
                object desc = DataBinder.Eval(DataItem, "Description");
				healthInfo.Description = Convert.ToString(desc);
				object remk = DataBinder.Eval(DataItem, "Remarks");
				healthInfo.Remarks = Convert.ToString(remk);
				IssueType = Convert.ToString(DataBinder.Eval(DataItem, "IssueType"));
			} else if (DataItem is KPIS.GERP.GHRMS.MODEL.HealthInfo)
			{
				healthInfo = (DataItem as HealthInfo);
				txtDescription.Text = healthInfo.Description;
				txtRemark.Text = healthInfo.Remarks;
				employeeSequence.Value = Convert.ToString(healthInfo.EmployeeSequence);
				healthIssueSeq.Value = Convert.ToString(healthInfo.Sequence);
				IssueType = healthInfo.IssueType;
			}
		}

		public string IssueType
		{
			get { return Convert.ToString(issueType.Value); }
			set
			{
				issueType.Value = value;
				switch (value)
				{
					case "Allergic":
                       
						lblHeader.Text = "ประวัติการแพ้ยา";
						IssueNameCaption.Text = "ชื่อยาที่แพ้";
						break;
					case "PersonalMedicine":
						lblHeader.Text = "ยาที่ใช้เป็นประจำ";
						IssueNameCaption.Text = "ชื่อยาที่ใช้ประจำ";
						break;
                    //case "UnderlyingDisease":
                    default: // UnderlyingDisease
                        lblHeader.Text = "โรคประจำตัว";
                        IssueNameCaption.Text = "ชื่อโรคประจำตัว";
                        break;
				}
			}
		}

		protected void btnCancel_Click(object sender, EventArgs e)
		{
            //Page.FindControl("RadMultiPage1").FindControl("PS_HisHealthIssue").Controls[0].FindControl("rblIssueType").Visible = true;
		}

		protected void btnUpdate_Click(object sender, EventArgs e)
		{

		}

		protected void btnAddnew_Click(object sender, EventArgs e)
		{

		}

		protected void submitInsert_Click(object sender, EventArgs e)
		{
			healthInfo.Employee = new EmployeeInfo(Convert.ToInt32(employeeSequence.Value));
			healthInfo.Description = txtDescription.Text;
			healthInfo.Remarks = txtRemark.Text;
			healthInfo.SaveDate = DateTime.Now;
			healthInfo.CreatedBy = ssMng.EmpSeqI;
			healthInfo.UpdatedBy = ssMng.EmpSeqI;
			healthInfo.UpdateDate = DateTime.Now;
			healthInfo.IssueType = IssueType;
			hhcBll.Insert(healthInfo);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
		}

		protected void ButtonUpdate_Click(object sender, EventArgs e)
		{
			healthInfo.Employee = new EmployeeInfo(Convert.ToInt32(employeeSequence.Value));
			healthInfo.Description = txtDescription.Text;
			healthInfo.Remarks = txtRemark.Text;
			healthInfo.SaveDate = DateTime.Now;
			healthInfo.UpdatedBy = ssMng.EmpSeqI;
			healthInfo.UpdateDate = DateTime.Now;
			healthInfo.IssueType = IssueType;
			healthInfo.Sequence = Convert.ToInt32(healthIssueSeq.Value);
			hhcBll.Update(healthInfo);
			main.MessageAlert(Page, "บันทึกข้อมูลแล้ว");
		} 

		public enum IssueTypeOption
		{
			UnderlyingDisease,
			Allergic,
			PersonalMedicine
		}
	}

}